/*
 * @Author: Quinn
 * @Date: 2023-05-18 11:31:42
 * @LastEditors: quinn
 * @Description:
 */
import store from "@/store/index";
import { LOGIN_PATH } from "@/assets/js/define";

const extender = (router) => {
	router.cacheRouter = [];
	router.beforeEach((to, from, next) => {
		const token = window.$quinn.getStorage("token") || store.state.token || "";
		const userData =
			window.$quinn.getStorage("userData") || store.state.userData || {};
		if (!token) window.location.href = LOGIN_PATH;
		else
			store.commit("modify", {
				token,
				userData,
			});
		const cacheRouter = store.state.cacheRouter;
		if (cacheRouter.every((e) => e.name !== to.name)) {
			cacheRouter.push(to);
			store.commit("modify", {
				cacheRouter,
			});
		}
		next();
	});
	return router;
};

export default extender;
